package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.message.MessageConstant;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.querypage.QueryPageBean;
import com.itheima.results.PageResult;
import com.itheima.results.Result;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {
    @Reference
    private CheckItemService checkItemService;

    //检查项分页查询
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findPage(queryPageBean);
    }

    //检查项添加数据
    @RequestMapping("/add.do")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.addCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //删除检查项数据
    @RequestMapping("/delete.do")
    public Result deleteCheckItem(Integer id) {
        //判断该检查项是否关联了主键(关联 不能删除)
        Long count = checkItemService.findCheckItemByID(id);
        if (count > 0) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        try {
            checkItemService.deleteCheckItem(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //回显编辑表单数据
    @RequestMapping("/findById.do")
    public Result findByID(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findByID(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    //编辑检查项数据
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    //查询所有检查项
    @RequestMapping("/findAll.do")
    public Result findAllCheckItem(){
        try {
            List<CheckItem> list = checkItemService.findAllCheckItem();
            return  new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}



